import axios from 'axios'
// import qs from 'qs'
const baseUrl = 'http://192.168.21.145:8088/rest/'
const api = Object.freeze({
  design: {
    getAllBean: baseUrl + 'org/BeanManagerService/getAllBean',
    fetchComFields: baseUrl + 'dscommon/TenantConfigService/findFieldsByBean',
    saveTotle: baseUrl + 'dscommon/DesMainService/saveDesVo?usertoken=admin',
    fetchTmpls: baseUrl + 'dscommon/DesMainService/query',
    fetchComps: baseUrl + 'dscommon/DesMainService/getDes'
  }
})
class Service {
  getAllBean (compId, usertoken) {
    return axios({
      url: api.design.getAllBean,
      params: {
        compId: compId,
        usertoken: usertoken
      },
      transformResponse: [data => {
        let _data = JSON.parse(data)
        if (_data.error) return _data.message
        let o = []
        for (let v of _data) {
          o.push({
            label: v.title,
            value: v.key
          })
        }
        return o
      }]
    })
  }
  fetchTmpls (queryName, userToken) {
    return axios({
      url: api.design.fetchTmpls,
      params: {
        queryName: queryName,
        usertoken: userToken
      },
      transformResponse: [data => {
        let _data = JSON.parse(data)
        if (_data.error) return _data.message
        if (!_data.length) return []
        let arr = []
        for (let v of _data) {
          arr.push({
            label: v.desName,
            value: v.desCode
          })
        }
        return arr
      }]
    })
  }
  fetchComps (desCode, userToken) {
    return axios({
      url: api.design.fetchComps,
      params: {
        desCode: desCode,
        usertoken: userToken
      }
    })
  }
  fetchComFields (beanname, usertoken) {
    return axios({
      url: api.design.fetchComFields,
      params: {
        beanname: beanname,
        usertoken: usertoken
      },
      transformResponse: [data => {
        data = JSON.parse(data)
        if (data.error) return data.message
        let ch = []
        for (let v of data) {
          let o = {
            label: v.title,
            value: v.beanField,
            dataType: v.dataType
          }
          !v['dataUrl'] ? ch.push(o) : ((o['dataUrl'] = v.dataUrl) && ch.push(o))
        }
        return ch
      }]
    })
  }
  saveTotle (conf) {
    return axios({
      method: 'POST',
      url: api.design.saveTotle,
      headers: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      data: conf
      // transformRequest: [data => {
      //   let _data = qs.stringify(data)
      //   console.log(_data, data)
      //   return _data
      // }]
    })
  }
}
export const getService = _ => new Service()
